package leetcode_1_200;

import node.TreeNode;

public class LeeCode_129 {
    public static void main(String[] args) {
        System.out.println(sumNumbers(new TreeNode(1, new TreeNode(2), new TreeNode(3))));
    }
    private static int sumNumbers(TreeNode root) {
        int[] res = new int[1];
        f(root, 0, res);
        return res[0];
    }
    private static void f(TreeNode node, int sum, int[] res){
        sum = sum * 10 + node.val;
        if (node.left == null && node.right == null){
            res[0] += sum;
        }
        if (node.left != null)
            f(node.left, sum, res);
        if (node.right != null)
            f(node.right, sum, res);
    }
}
